/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.plugin.error;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author lukasz
 */
public class ErrorManagerGUIComponent extends JPanel
{
    private ErrorManagerPlugin manager;
    private ErrorDescription desc = null;

    private JPanel topPanel;
    private JLabel headerLabel;
    private JPanel stackPanel;
    private JCheckBox stackCheckBox;
    private JButton stackButton;
    
    private JPanel exceptionPanel;
    private JLabel classLabel;
    private JTextArea messageArea;
    private JScrollPane messageScroll;

    private JPanel locationPanel;
    private JLabel locationLabel;

    


    /**
     *
     * @param manager
     */
    public ErrorManagerGUIComponent(ErrorManagerPlugin manager)
    {
        final ErrorManagerGUIComponent myself = this;

        this.manager = manager;

        locationLabel = new JLabel();
        locationLabel.setFont(new Font("verdana", Font.BOLD, 12));
        
        locationPanel = new JPanel(new BorderLayout());
        locationPanel.add(locationLabel, BorderLayout.CENTER);

        classLabel = new JLabel();
        classLabel.setFont(new Font("verdana", Font.BOLD, 14));

        messageArea = new JTextArea();
        messageArea.setEditable(false);
        messageScroll = new JScrollPane();
        messageScroll.getViewport().add(messageArea);

        exceptionPanel = new JPanel(new BorderLayout());
        exceptionPanel.add(classLabel, BorderLayout.NORTH);
        exceptionPanel.add(messageScroll, BorderLayout.CENTER);

        stackCheckBox = new JCheckBox("Filter", true);
        stackButton = new JButton("View Stack");
        stackButton.setEnabled(false);
        stackButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (desc == null)
                    return;

                if (stackCheckBox.isSelected())
                    displayStackTrace(desc.getShortStackTrace());
                else
                    displayStackTrace(desc.getFullStackTrace());
            }
        });

        stackPanel = new JPanel(new BorderLayout());
        stackPanel.add(stackCheckBox, BorderLayout.EAST);
        stackPanel.add(stackButton, BorderLayout.WEST);

        headerLabel = new JLabel("NO ERRORS");
        headerLabel.setFont(new Font("verdana", Font.BOLD, 16));

        topPanel = new JPanel(new BorderLayout());
        topPanel.add(headerLabel, BorderLayout.WEST);
        topPanel.add(stackPanel, BorderLayout.EAST);

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(locationPanel, BorderLayout.SOUTH);
        add(exceptionPanel, BorderLayout.CENTER);

    }

    /**
     * 
     * @param desc
     */
    public void setErrorDescription(ErrorDescription desc)
    {
        this.desc = desc;

        if (desc != null)
        {
            if (desc.isCompileTime())
            {
                headerLabel.setText("COMPILE ERROR");
                stackCheckBox.setEnabled(false);
                stackButton.setEnabled(false);
            }
            else
            {
                headerLabel.setText("RUNTIME ERROR");
                stackCheckBox.setEnabled(true);
                stackButton.setEnabled(true);
            }

            classLabel.setText("Exception type: " + desc.getExceptionType());
            messageArea.setText(desc.getExceptionMessage());

            if (!desc.isCompileTime())
                locationLabel.setText("LOCATION: method " + desc.getMethodName()
                                      + " in class " + desc.getClassName() +
                                      " (" + desc.getFilename() + ", line " +
                                      desc.getLineNumber() + ")");
            else
                locationLabel.setText("LOCATION: line " + desc.getLineNumber() +
                                      " in file " + desc.getFilename());
        }
        else
        {
            classLabel.setText("");
            messageArea.setText("");

            headerLabel.setText("NO ERRORS");
            locationLabel.setText("");
            stackButton.setEnabled(false);
            stackCheckBox.setEnabled(false);
        }
    }


    private void displayStackTrace(String stackTrace)
    {
        JTextArea area = new JTextArea(stackTrace);
        JScrollPane scroll = new JScrollPane();
        scroll.getViewport().add(area);

        final JFrame win = new JFrame("Stack Trace");
        win.setLayout(new BorderLayout());
        win.add(scroll, BorderLayout.CENTER);

        JButton button = new JButton("Close");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                win.dispose();
            }
        });
        win.add(button, BorderLayout.SOUTH);

        win.setPreferredSize(new Dimension(300, 400));
        win.pack();
        win.setVisible(true);
    }
}
